<template>
  <div class="role">
    <div class="search">
      <page-search :config="formConfig" :btnConfig="btnConfig"></page-search>
    </div>
    <div class="table">
      <page-content
        page-name="role"
        headerBtnTxt="创建角色"
        :contentConfig="contentConfig"
        @header-handler-click="handleNewData"
        @edit-click="editClick"
      ></page-content>
    </div>
    <div class="dialog">
      <yc-dialog
        :config="dialogFormConfig"
        :dialog-title="dialogTitle"
        :other-info="otherInfo"
        page-name="role"
        :modalConfig="defaultInfo"
        ref="dialogRef"
      >
        <div class="menuTree">
          <el-tree
            ref="elTreeRef"
            :data="menuTreeData"
            :props="props"
            node-key="id"
            @check="handleCheckChange"
            show-checkbox
          />
        </div>
      </yc-dialog>
    </div>
  </div>
</template>

<script lang="ts">
import { ElTree } from 'element-plus'

import { computed, defineComponent, ref, nextTick, getCurrentInstance, onMounted } from 'vue'
import { useStore } from '@/store'

import pageContent from '@/base-ui/page-content'
import pageSearch from '@/base-ui/page-search'
import ycDialog from '@/base-ui/dialog'

// 配置
import { contentConfig } from './config/table-config'
import { formConfig, btnConfig } from './config/search-config'
import { dialogFormConfig } from './config/dialog-config'

// hooks
import { usePageDialog } from '@/hooks/usePageDialog'
import { getMenuLeafKeys } from '@/utils/map-menus'

export default defineComponent({
  name: 'role',
  components: {
    pageContent,
    pageSearch,
    ycDialog
  },
  setup() {
    const store = useStore()
    const elTreeRef = ref<InstanceType<typeof ElTree>>()

    const { handleNewData, editClick, defaultInfo, dialogRef, dialogTitle } = usePageDialog(
      undefined,
      editCallBack
    )

    const menuTreeData = computed(() => store.state.entireMenu)
    const otherInfo = ref({})
    const props = {
      label: 'name',
      children: 'children'
    }
    function handleCheckChange(row: any, node: any) {
      console.log(row, node)
      const checkedKeys = node.checkedKeys
      const halfCheckedKeys = node.halfCheckedKeys
      const menuList = [...checkedKeys, ...halfCheckedKeys]
      otherInfo.value = { menuList }
    }
    onMounted(() => {
      console.log(elTreeRef)
    })
    function editCallBack(item: any) {
      console.log(item, 'item')
      const { menuList } = item

      const leafKeys = getMenuLeafKeys(menuList)

      setTimeout(() => {
        console.log(elTreeRef)
        elTreeRef.value?.setCheckedKeys(leafKeys, false)
      })

      // setTimeout(() => {
      //   elTreeRef.value?.setCheckedKeys(leafKeys, false)
      //   // console.log(leafKeys, elTreeRef.value)
      // }, 0)
    }

    return {
      contentConfig,
      formConfig,
      btnConfig,
      dialogFormConfig,
      handleNewData,
      editClick,
      defaultInfo,
      dialogRef,
      dialogTitle,
      menuTreeData,
      props,
      otherInfo,
      handleCheckChange,
      editCallBack,
      elTreeRef
    }
  }
})
</script>

<style scoped lang="less">
.dialog {
  .menuTree {
    margin-left: 25px;
  }
}
</style>
